package com.java.diary;

import com.java.diary.dao.Diary;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class UpdateJFrame extends JFrame implements ActionListener {
    JTextField subjectText  = new JTextField();
    JTextArea contentText = new JTextArea();

    JButton update = new JButton("修改");
    JButton cancel = new JButton("取消");

    ArrayList<Diary> diaries;
    int index;

    public UpdateJFrame(){}

    public UpdateJFrame(ArrayList<Diary> diaries,int index){
        this.diaries = diaries;
        this.index = index;
        subjectText.setText(diaries.get(index).getTitle());
        contentText.setText(diaries.get(index).getContent());
        //初始化界面
        initFrame();
        //初始化组件
        initView();
        //让界面展示出来
        this.setVisible(true);
    }

    private void initView() {
        //定义大标题
        JLabel title = new JLabel("每日一记");
        title.setBounds(220, 20, 584, 50);
        title.setFont(new Font("宋体", Font.BOLD, 32));
        this.getContentPane().add(title);

        //标题
        JLabel subject = new JLabel("标题");
        subject.setBounds(70,90,100,30);
        subject.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(subject);

        subjectText.setBounds(120,90,426,30);
        subjectText.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(subjectText);

        //内容
        JLabel content = new JLabel("内容");
        content.setBounds(70,140,100,30);
        content.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(content);

        contentText.setBounds(120,140,426,300);
        contentText.setFont(new Font("宋体",Font.PLAIN,16));
        this.getContentPane().add(contentText);

        //保存
        update.setBounds(132,466,140,40);
        update.setFont(new Font("宋体",Font.PLAIN,24));
        update.addActionListener(this);
        this.getContentPane().add(update);

        //取消
        cancel.setBounds(312,466,140,40);
        cancel.setFont(new Font("宋体",Font.PLAIN,24));
        cancel.addActionListener(this);
        this.getContentPane().add(cancel);
    }

    //对添加界面的一些设置
    private void initFrame() {
        this.setSize(600, 600);
        this.setTitle("每日一记（添加日记）");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.getContentPane().setBackground(new Color(212,212,212));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == update){
            diaries.get(index).setTitle(subjectText.getText());
            diaries.get(index).setContent(contentText.getText());
            new NoteJFrame();
            this.setVisible(false);
        }else if(obj == cancel){
            this.setVisible(false);
            new NoteJFrame();
        }
    }
}
